<?php
Loader::includeClass('interface/exceptionmanager.php') ;

final class Core extends Cosmo {
	private $_controller 	= null ;
	private $_method 		= null ;
	private $_data 		= null ;
	private $_param	 	= null ;
	private $_form 		= null ;
	private $_url 			= null ;
	private $_oController	= false ;
	private static $_instance= null ;
	private $template		= null ;
	
	
	/**
	 * Core::getInstance()
	 * 
	 * @desc Allow to create or return instance of the Core class
	 * 
	 * @return object
	 */
	public static function getInstance() {		
		if (is_null(self::$_instance))
			self::$_instance = new Core();
    		return self::$_instance;
	}
	
	public function __construct() {
		parent::__construct() ;
		
		$this->template =& $this->load->object('template') ;
		
		if(isset($_GET['c']) && !empty($_GET['c']))
			$this->_controller 	= strtolower($_GET['c']) ;
		if(isset($_GET['m']) && !empty($_GET['m']))
			$this->_method 	= strtolower($_GET['m']) ;
		if(isset($_GET['data']) && !empty($_GET['data']))
			$this->_data 		= $_GET['data'] ;
		if(isset($_GET['param']) && !empty($_GET['param']))
			$this->_param		= $_GET['param'] ;
		if(isset($_POST['form']) && !empty($_GET['form']))
			$this->_form		= $_POST['form'] ;
		
		if(is_null($this->_controller))
			$this->_controller = DEFAULT_CONTROLLER ;
			
		if(is_null($this->_method))
			$this->_method = 'index' ;
			
		$this->_url = BASE_URL.$_SERVER['PHP_SELF'] ;
		unset($_GET, $_POST['form']) ;
	}
	public function getContext($type) {
		switch(strtolower($type)) {
			case 'controller' :
				return $this->_controller;
			case 'method' :
				return $this->_method;
			case 'data' :
				return $this->_data;
			case 'form' :
				return $this->_form;
			case 'param' :
				return $this->_param;
			case 'url' :
				return $this->_url;
			default :
				return null;
		}
	}
	public function setContext($type, $content) {
		switch(strtolower($type)) {
			case 'method' :
				$this->_method = $content ;
				$this->template->assign('method', $content) ;
				return true;
			case 'data' :
				$this->_data = $content ;
				$this->template->assign('data', $content) ;
				return true;
			default :
				return false;
		}
	}
	public function execute() {	
		$this->_oController =& $this->load->controller($this->_controller) ;
		if($this->_oController === false) {
			$this->sendError('Controller '.$this->_controller.' that you requested does not exists');
			return false;
		}
		
		$this->template->assign('controller', strtolower(get_class($this->_oController))) ;
		if(false === $this->_oController->_init()) {
			$this->sendError($this->_oController->getError()) ;
		}
		$this->_oController->_map() ;
	}
	public function _checkException($msg) {
		switch(strtoupper($msg)) {
			case 'NO_CONTROLLER' :
				$this->sendError('Controller '.$this->_controller.' that you requested does not exists');
		}
	}
	public function initSite() {
		$this->template->add(HEADER_TEMPLATE) ;
	}
	public function display() {
		if($this->template->isDisplay() === false) {
			$this->_oController->_output() ;
			$this->template->setPath('all','') ; // reset to default template directories
			$this->template->add(FOOTER_TEMPLATE) ; 
			$this->template->display() ;
		}
	}
	public function __destruct() {
		
	}
}
?>